extends BaseEnemy

class_name Slime

# Slime特定属性
@export var jump_force: float = 150.0
@export var jump_cooldown: float = 2.0

var jump_timer: float = 0.0
var idle_texture: Texture2D
var run_texture: Texture2D

func _ready():
	# 设置Slime属性
	max_health = 30
	speed = 40.0
	damage = 8
	detection_range = 60.0
	attack_range = 15.0
	
	# 加载纹理
	idle_texture = load("res://Art/v1.1 dungeon crawler 16X16 pixel pack/enemies/slime/slime_idle_spritesheet.png")
	run_texture = load("res://Art/v1.1 dungeon crawler 16X16 pixel pack/enemies/slime/slime_run_spritesheeet.png")
	
	super._ready()
	create_animations()

func create_animations():
	if not animation_player:
		return
	
	# 使用父类的安全函数获取或创建动画库
	var library = get_or_create_animation_library()
	
	# 创建idle动画（只在不存在时创建）
	if not library.has_animation("idle"):
		var idle_animation = Animation.new()
		idle_animation.length = 1.0
		idle_animation.loop_mode = Animation.LOOP_LINEAR
		
		# 添加帧动画轨道
		var track_index = idle_animation.add_track(Animation.TYPE_VALUE)
		idle_animation.track_set_path(track_index, NodePath("Sprite2D:frame"))
		idle_animation.track_insert_key(track_index, 0.0, 0)
		idle_animation.track_insert_key(track_index, 0.17, 1)
		idle_animation.track_insert_key(track_index, 0.33, 2)
		idle_animation.track_insert_key(track_index, 0.5, 3)
		idle_animation.track_insert_key(track_index, 0.67, 4)
		idle_animation.track_insert_key(track_index, 0.83, 5)
		
		library.add_animation("idle", idle_animation)
	
	# 创建run动画（只在不存在时创建）
	if not library.has_animation("run"):
		var run_animation = Animation.new()
		run_animation.length = 0.6
		run_animation.loop_mode = Animation.LOOP_LINEAR
		
		# 添加帧动画轨道
		var run_track = run_animation.add_track(Animation.TYPE_VALUE)
		run_animation.track_set_path(run_track, NodePath("Sprite2D:frame"))
		run_animation.track_insert_key(run_track, 0.0, 0)
		run_animation.track_insert_key(run_track, 0.1, 1)
		run_animation.track_insert_key(run_track, 0.2, 2)
		run_animation.track_insert_key(run_track, 0.3, 3)
		run_animation.track_insert_key(run_track, 0.4, 4)
		run_animation.track_insert_key(run_track, 0.5, 5)
		
		library.add_animation("run", run_animation)

func _physics_process(delta):
	jump_timer += delta
	super._physics_process(delta)
	update_animation()

func handle_chase_state(delta):
	if player:
		var direction = (player.global_position - global_position).normalized()
		
		# Slime特有的跳跃移动
		if jump_timer >= jump_cooldown:
			velocity = direction * jump_force
			jump_timer = 0.0
		else:
			velocity = velocity.move_toward(Vector2.ZERO, 100.0 * delta)
		
		var distance_to_player = global_position.distance_to(player.global_position)
		if distance_to_player <= attack_range and attack_timer <= 0:
			change_state(State.ATTACK)

func update_animation():
	if is_dead:
		return
		
	match current_state:
		State.IDLE:
			sprite.texture = idle_texture
			if animation_player.current_animation != "idle":
				safe_play_animation("idle")
		State.CHASE:
			if velocity.length() > 10:
				sprite.texture = run_texture
				if animation_player.current_animation != "run":
					safe_play_animation("run")
			else:
				sprite.texture = idle_texture
				if animation_player.current_animation != "idle":
					safe_play_animation("idle")
					
